#!/usr/bin/env bash

source ./config/env.sh
clear


mkdir -p ${K8S_DATA}/config/heapster/yaml


cp -R ${K8S_DATA}/unpack/heapster-1.5.4/deploy ${K8S_DATA}/config/heapster/yaml

cp ${K8S_DATA}/config/heapster/yaml/deploy/kube-config/influxdb/grafana.yaml{,.orig}
sed -i "s/# type: NodePort/type: NodePort/g" ${K8S_DATA}/config/heapster/yaml/deploy/kube-config/influxdb/grafana.yaml
sed -i "s#gcr.io/google_containers/heapster-grafana-amd64:v4.4.3#mirrorgooglecontainers/heapster-grafana-amd64:v4.4.3#g" ${K8S_DATA}/config/heapster/yaml/deploy/kube-config/influxdb/grafana.yaml
#sed -i "s#gcr.io/google_containers/heapster-grafana-amd64:v4.4.3#mirrorgooglecontainers/heapster-grafana-amd64:v5.0.4#g" ${K8S_DATA}/config/heapster/yaml/deploy/kube-config/influxdb/grafana.yaml

cp ${K8S_DATA}/config/heapster/yaml/deploy/kube-config/influxdb/heapster.yaml{,.orig}
sed -i "s#https://kubernetes.default#https://kubernetes.default?kubeletHttps=true\&kubeletPort=10250#g" ${K8S_DATA}/config/heapster/yaml/deploy/kube-config/influxdb/heapster.yaml
sed -i "s#gcr.io/google_containers/heapster-amd64:v1.5.3#mirrorgooglecontainers/heapster-amd64:v1.5.4#g" ${K8S_DATA}/config/heapster/yaml/deploy/kube-config/influxdb/heapster.yaml
#sed -i "s#gcr.io/google_containers/heapster-amd64:v1.5.3#mirrorgooglecontainers/heapster-amd64:v1.5.4#g" ${K8S_DATA}/config/heapster/yaml/deploy/kube-config/influxdb/heapster.yaml

cp ${K8S_DATA}/config/heapster/yaml/deploy/kube-config/influxdb/influxdb.yaml{,.orig}
sed -i "s#gcr.io/google_containers/heapster-influxdb-amd64:v1.3.3#mirrorgooglecontainers/heapster-influxdb-amd64:v1.5.2#g" ${K8S_DATA}/config/heapster/yaml/deploy/kube-config/influxdb/influxdb.yaml
#sed -i "s#gcr.io/google_containers/heapster-influxdb-amd64:v1.3.3#mirrorgooglecontainers/heapster-influxdb-amd64:v1.5.2#g" ${K8S_DATA}/config/heapster/yaml/deploy/kube-config/influxdb/influxdb.yaml

cp ${K8S_DATA}/config/heapster/yaml/deploy/kube-config/rbac/heapster-rbac.yaml{,.orig}
cat > ${K8S_DATA}/config/heapster/yaml/deploy/kube-config/rbac/heapster-rbac.yaml << EOF
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
  name: heapster-kubelet-api
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: system:kubelet-api-admin
subjects:
- kind: ServiceAccount
  name: heapster
  namespace: kube-system
EOF


cd ${K8S_DATA}/config/heapster/yaml/deploy/kube-config/influxdb
${K8S_BIN_DIR}/kubectl create -f .

${K8S_BIN_DIR}/kubectl create -f ${K8S_DATA}/config/heapster/yaml/deploy/kube-config/rbac/heapster-rbac.yaml

${K8S_BIN_DIR}/kubectl get pods -n kube-system | grep -E 'heapster|monitoring'
${K8S_BIN_DIR}/kubectl get svc -n kube-system|grep -E 'monitoring|heapster'